unit OdontoCadTabelaPreco;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, CadastroPadrao, Vcl.Menus, Vcl.ComCtrls,
  Vcl.Buttons, Vcl.ExtCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, Padroes,
  Vcl.Mask, Vcl.DBCtrls, OdontoDB, Data.DB, Data.Win.ADODB;

type
  TformTabelaPreco = class(TformCadastro)
    OdontoLabel1: TOdontoLabel;
    OdontoLabel3: TOdontoLabel;
    OdontoLabel4: TOdontoLabel;
    OdontoLabel5: TOdontoLabel;
    OdontoLabel6: TOdontoLabel;
    OdontoLabel7: TOdontoLabel;
    qCadastro: TADOQuery;
    qConvenio: TADOQuery;
    dsCadastro: TDataSource;
    dsConvenio: TDataSource;
    dblVersaoProc: TDBLookupOdontoEdit;
    dbeProcedimento: TDBOdontoEdit;
    dblProcedimento: TDBLookupOdontoEdit;
    dbePreco: TDBOdontoEdit;
    dblConvenio: TDBLookupOdontoEdit;
    dblPlano: TDBLookupOdontoEdit;
    qTipoPlano: TADOQuery;
    dsTipoPlano: TDataSource;
    qConvenioCon_Cd: TIntegerField;
    qConvenioCon_Ds: TStringField;
    qTipoPlanoTpp_Cd: TIntegerField;
    qTipoPlanoTpp_Ds: TStringField;
    qGrid: TADOQuery;
    dsGrid: TDataSource;
    dsVersaoProcedimento: TDataSource;
    qVersaoProcedimento: TADOQuery;
    qProcedimento: TADOQuery;
    dsProcedimento: TDataSource;
    qGridPrp_Cd: TIntegerField;
    qGridPrp_TipoPlano: TIntegerField;
    qGridPrp_VersaoProcedimento: TStringField;
    qGridPrp_Procedimento: TStringField;
    qGridPrp_Preco: TBCDField;
    qGridTpp_Ds: TStringField;
    qGridPro_Ds: TStringField;
    qGridCon_Ds: TStringField;
    qCadastroPrp_Cd: TAutoIncField;
    qCadastroPrp_TipoPlano: TIntegerField;
    qCadastroPrp_Ds: TStringField;
    qCadastroPrp_VersaoProcedimento: TStringField;
    qCadastroPrp_Procedimento: TStringField;
    qCadastroPrp_Preco: TBCDField;
    OdontoLabel8: TOdontoLabel;
    dbeCodigo: TDBOdontoEdit;
    qCadastroPrp_Convenio: TIntegerField;
    qGridPrp_Convenio: TIntegerField;
    qDinamica: TADOQuery;
    edtCodigo: TOdontoNumero;
    procedure FormCreate(Sender: TObject);
    procedure btnInclusaoClick(Sender: TObject);
    procedure btnAlteracaoClick(Sender: TObject);
    procedure btnCancelaClick(Sender: TObject);
    procedure btnPesquisarClick(Sender: TObject);
    procedure btnConfirmaClick(Sender: TObject);
    procedure btnExclusaoClick(Sender: TObject);
    procedure dblConvenioCloseUp(Sender: TObject);
  private
  public
  end;

var
  formTabelaPreco: TformTabelaPreco;

implementation

{$R *.dfm}

uses Definicoes, Funcoes;

procedure TformTabelaPreco.btnAlteracaoClick(Sender: TObject);
begin
   qCadastro.Close;
   qCadastro.Parameters.ParamByName('Prp_Cd').Value := qGridPrp_Cd.AsInteger;
   qCadastro.Open;
   qConvenio.Close;
   qConvenio.Open;
   qTipoPlano.Close;
   qTipoPlano.Open;
   if qCadastro.IsEmpty then
      Exit;
   qCadastro.Edit;
   inherited;
   dblConvenio.SetFocus;
end;

procedure TformTabelaPreco.btnCancelaClick(Sender: TObject);
begin
   inherited;
   qCadastro.Cancel;
end;

procedure TformTabelaPreco.btnConfirmaClick(Sender: TObject);
begin

   if ( Trim(dblPlano.Text) = EmptyStr ) then begin
      Mensagem('Informe o campo Plano');
      dblPlano.SetFocus;
      Exit;
   end;

   try
      qCadastro.Post;
   except
      on e: Exception do begin
         MessageDlg(TratarErro(e.Message), mtInformation, [mbOK], 0);
         Exit;
      end;
   end;
   btnPesquisarClick(Sender);
   inherited;
end;

procedure TformTabelaPreco.btnExclusaoClick(Sender: TObject);
begin
   inherited;
   if Confirma(MSG_EXCLSUSAO_REGISTRO) then begin
      qCadastro.Close;
      qCadastro.Parameters.ParamByName('Prp_Cd').Value := qGridPrp_Cd.AsInteger;
      qCadastro.Open;
      try
         qCadastro.Delete;
      except
         MessageDlg(MSG_ERRO_EXCLUIR_REGISTRO, mtInformation, [mbOK], 0);
         Exit;
      end;
      btnPesquisarClick(Sender);
   end;
end;

procedure TformTabelaPreco.btnInclusaoClick(Sender: TObject);
begin
   inherited;
   qCadastro.Close;
   qCadastro.Parameters.ParamByName('Prp_Cd').Value := Null;
   qCadastro.Open;
   qCadastro.Insert;
   dblConvenio.SetFocus;
end;

procedure TformTabelaPreco.btnPesquisarClick(Sender: TObject);
begin
   inherited;
   qGrid.Close;
   qGrid.SQL.Clear;
   qGrid.SQL.Add(' SELECT Prp_Cd, ');
   qGrid.SQL.Add('        Prp_TipoPlano, ');
   qGrid.SQL.Add('        Prp_VersaoProcedimento, ');
   qGrid.SQL.Add('        Prp_Procedimento, ');
   qGrid.SQL.Add('        Prp_Preco, ');
   qGrid.SQL.Add('        TipoPlano.Tpp_Ds, ');
   qGrid.SQL.Add('        Procedimento.Pro_Ds, ');
   qGrid.SQL.Add('        Convenio.Con_Ds, ');
   qGrid.SQL.Add('        Prp_Convenio ');
   qGrid.SQL.Add(' FROM PrecoPlano ');
   qGrid.SQL.Add('   JOIN TipoPlano ON Tpp_Cd = Prp_TipoPlano ');
   qGrid.SQL.Add('   JOIN Procedimento ON Pro_Cd = Prp_Procedimento ');
   qGrid.SQL.Add('                     AND Pro_Versao = Prp_VersaoProcedimento ');
   qGrid.SQL.Add('   JOIN Convenio ON Con_Cd = Prp_Convenio ');
   qGrid.SQL.Add(' WHERE Prp_Cd IS NOT NULL ');
   if ( Trim(edtCodigo.Text) <> EmptyStr ) then
      qGrid.SQL.Add(' AND Prp_Cd = ' + edtCodigo.Text );
   qGrid.Open;
end;

procedure TformTabelaPreco.dblConvenioCloseUp(Sender: TObject);
begin
   inherited;
   if ( dblConvenio.KeyValue <> Null ) and ( dblConvenio.KeyValue <> qCadastroPrp_Convenio.OldValue ) then
      dblPlano.KeyValue := Null;
end;

procedure TformTabelaPreco.FormCreate(Sender: TObject);
begin
   inherited;
   qCadastro.Open;
   qConvenio.Open;
   qTipoPlano.Open;
   qVersaoProcedimento.Open;
   qProcedimento.Open;
end;

end.

